home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Tools / twit / mac_widgets.py next >
Encoding:
Python Source  |  2000-06-23  |  8.8 KB  |  318 lines

  1. from FrameWork import *
  2. import Win
  3. import Qd
  4. import Controls
  5. import Ctl
  6. import TE
  7. import List
  8. import os
  9. import string
  10. import macfs
  11.  
  12. SCROLLBAR=16
  13. MARGIN=2
  14. ICONSIZE=16
  15. TEXTWIDTH=4096 # More-or-less random value
  16.  
  17. TEXTFONT=4
  18. TEXTSIZE=9
  19.  
  20. # Resource numbers
  21. PIC_CURRENT=500
  22. PIC_BREAK=501
  23.  
  24. picture_cache={}
  25.  
  26. class MT_TextWidget:
  27.     def __init__(self, wid, r):
  28.         self.wid = wid
  29.         self.rect = r
  30.         left, top, right, bottom = r
  31.         self.terect = left+MARGIN+ICONSIZE, top+MARGIN, \
  32.                 right-(MARGIN+SCROLLBAR), bottom-(MARGIN+SCROLLBAR)
  33.         dr = self.terect[0], self.terect[1], TEXTWIDTH, self.terect[3]
  34.         Qd.SetPort(wid)
  35.         Qd.TextFont(TEXTFONT)
  36.         Qd.TextSize(TEXTSIZE)
  37.         self.ted = TE.TENew(dr, self.terect)
  38.         self.ted.TEAutoView(1)
  39.         self.activate(1)
  40.         
  41.         rect = right-SCROLLBAR, top, right, bottom-SCROLLBAR+1
  42.         self.bary = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0)
  43.         rect = left, bottom-SCROLLBAR, right-SCROLLBAR+1, bottom
  44.         self.barx = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0)
  45.         
  46.         self.have_data = 0
  47.         self.line_index = []
  48.         
  49.     def close(self):
  50.         del self.barx
  51.         del self.bary
  52.         del self.ted
  53.         
  54.     def scrollbars(self):
  55.         pass
  56.         
  57.     def setcontent(self, file):
  58.         self.line_index = []
  59.         if file == None:
  60.             data = ''
  61.             self.have_data = 0
  62.         else:
  63.             try:
  64.                 fp = open(file, 'rb') # NOTE the binary
  65.                 data = fp.read()
  66.                 self.have_data = 1
  67.             except IOError, arg:
  68.                 data = 'Cannot open file:\r'+`arg`
  69.                 self.have_data = 0
  70.         if len(data) > 32767:
  71.             self.have_data = 0
  72.             data = 'File too big'
  73.         self.ted.TESetText(data)
  74.         if self.have_data:
  75.             cur = 0
  76.             while 1:
  77.                 self.line_index.append(cur)
  78.                 try:
  79.                     cur = string.index(data, '\r', cur+1)
  80.                 except ValueError:
  81.                     break
  82.             self.line_index.append(len(data))
  83.         Win.InvalRect(self.rect)
  84.         self.ted.TESetSelect(0,0)
  85.         self.ted.TECalText()
  86.         self.ted.TESelView()
  87.         self.setscrollbars()
  88.         
  89.     def setscrollbars(self):
  90.         docleft, doctop, docright, docbot = self.ted.destRect
  91.         winleft, wintop, winright, winbot = self.ted.viewRect
  92.         docbot = self.ted.nLines*self.ted.lineHeight + doctop
  93.         self.setbar(self.barx, docleft, docright, winleft, winright)
  94.         self.setbar(self.bary, doctop, docbot, wintop, winbot)
  95.         
  96.     def setbar(self, bar, minmin, maxmax, curmin, curmax):
  97.         if maxmax-minmin > 32767 or (curmin <= minmin and curmax >= maxmax):
  98.             bar.SetControlMinimum(0)
  99.             bar.SetControlMaximum(0)
  100.             bar.SetControlValue(0)
  101.             return
  102.         bar.SetControlMinimum(minmin)
  103.         bar.SetControlMaximum(maxmax-(curmax-curmin))
  104.         bar.SetControlValue(curmin)
  105.  
  106.     def update(self, rgn):
  107.         Qd.EraseRect(self.terect)
  108.         Qd.FrameRect(self.rect)
  109.         self.ted.TEUpdate(self.terect)
  110.         
  111.     def activate(self, onoff):
  112.         if onoff:
  113.             self.ted.TEActivate()
  114.         else:
  115.             self.ted.TEDeactivate()
  116.  
  117.     def select(self, line):
  118.         if line == None or line <= 0 or not self.have_data:
  119.             self.ted.TESetSelect(0,0)
  120.         else:
  121.             line = line - 1
  122.             if line > len(self.line_index)-1: line = len(self.line_index)-1
  123.             if line == 1:
  124.                 self.ted.TESetSelect(0, self.line_index[1])
  125.             else:
  126.                 self.ted.TESetSelect(self.line_index[line]+1, self.line_index[line+1])
  127.         self.setscrollbars()
  128.         
  129.     def click(self, where, modifiers):
  130.         # First check scrollbars
  131.         ctltype, control = Ctl.FindControl(where, self.wid)
  132.         if ctltype and control:
  133.             partcode = control.TrackControl(where)
  134.             if partcode:
  135.                 self.controlhit(control, partcode)
  136.             return None, 0
  137.         off = self.ted.TEGetOffset(where)
  138.         inborder = where[0] < self.terect[0]
  139.         l, t, r, b = self.terect
  140.         if l <= where[0] <= r and t <= where[1] <= b or inborder:
  141.             return self.offsettoline(off), inborder
  142.         return None, 0    # In the grow box or something.
  143.         
  144.     def offsettoline(self, offset):
  145.         for i in range(len(self.line_index)):
  146.             if offset < self.line_index[i]:
  147.                 return i   # Not i-1: 1-based line numbers in files
  148.         return None
  149.  
  150.     def controlhit(self, control, partcode):
  151.         if partcode <> Controls.inThumb:
  152.             if control == self.barx:
  153.                 if partcode == Controls.inUpButton:
  154.                     delta = -10
  155.                 if partcode == Controls.inDownButton:
  156.                     delta = 10
  157.                 if partcode == Controls.inPageUp:
  158.                     delta = 10-(self.terect[2]-self.terect[0])
  159.                 if partcode == Controls.inPageDown:
  160.                     delta = (self.terect[2]-self.terect[0])-10
  161.                 old = control.GetControlValue()
  162.                 control.SetControlValue(old+delta)
  163.             if control == self.bary:
  164.                 if partcode == Controls.inUpButton:
  165.                     delta = -self.ted.lineHeight
  166.                 if partcode == Controls.inDownButton:
  167.                     delta = self.ted.lineHeight
  168.                 if partcode == Controls.inPageUp:
  169.                     delta = self.ted.lineHeight-(self.terect[3]-self.terect[1])
  170.                 if partcode == Controls.inPageDown:
  171.                     delta = (self.terect[3]-self.terect[1])-self.ted.lineHeight
  172.                 old = control.GetControlValue()
  173.                 control.SetControlValue(old+delta)
  174.         newx = self.barx.GetControlValue()
  175.         newy = self.bary.GetControlValue()
  176.         oldx = self.ted.viewRect[0]
  177.         oldy = self.ted.viewRect[1]
  178.         self.ted.TEPinScroll(oldx-newx, oldy-newy)
  179.         self.setscrollbars() # XXXX Bibbert, maar hoe anders?
  180.             
  181. class MT_IconTextWidget(MT_TextWidget):
  182.     def __init__(self, wid, r):
  183.         MT_TextWidget.__init__(self, wid, r)
  184.         self.breakpointlist = []
  185.         self.curline = None
  186.         self.iconrect = (self.rect[0]+1, self.rect[1]+1, 
  187.                 self.terect[0]-1, self.rect[3]-SCROLLBAR)
  188.         self.curlinerange = (self.terect[1]+self.ted.lineHeight,
  189.                 self.terect[3]-2*self.ted.lineHeight)
  190.         self.piccurrent = PIC_CURRENT
  191.         
  192.     def setbreaks(self, list):
  193.         self.breakpointlist = list[:]
  194.         Qd.SetPort(self.wid)
  195.         Win.InvalRect(self.iconrect)
  196.         
  197.     def setcurline(self, line, pic=PIC_CURRENT):
  198.         self.curline = line
  199.         self.piccurrent = pic
  200.         Qd.SetPort(self.wid)
  201.         self.showline(line)
  202.  
  203.     def showline(self, line):
  204.         if line <= 0: line = 1
  205.         if line >= len(self.line_index): line = len(self.line_index)-1
  206.         if line < 0: return
  207.         off = self.line_index[line]
  208.         x, y = self.ted.TEGetPoint(off)
  209.         if self.curlinerange[0] <= y <= self.curlinerange[1]:
  210.             return # It is in view
  211.         middle = (self.curlinerange[0]+self.curlinerange[1])/2
  212.         self.ted.TEPinScroll(0, middle-y) # Of andersom?
  213.         self.setscrollbars()
  214.         
  215.     def setscrollbars(self):
  216.         MT_TextWidget.setscrollbars(self)
  217.         Win.InvalRect(self.iconrect)
  218.                 
  219.     def update(self, rgn):
  220.         MT_TextWidget.update(self, rgn)
  221.         self.drawallicons()
  222.         
  223.     def drawallicons(self):
  224.         Qd.EraseRect(self.iconrect)
  225.         Qd.MoveTo(self.iconrect[2], self.iconrect[1])
  226.         Qd.LineTo(self.iconrect[2], self.iconrect[3])
  227.         topoffset = self.ted.TEGetOffset((self.terect[0], self.terect[1]))
  228.         botoffset = self.ted.TEGetOffset((self.terect[0], self.terect[3]))
  229.         topline = self.offsettoline(topoffset)
  230.         botline = self.offsettoline(botoffset)
  231.         if topline == None: topline = 1 # ???
  232.         if botline == None: botline = len(self.line_index)
  233.         for i in self.breakpointlist:
  234.             if topline <= i <= botline:
  235.                 self.draw1icon(i, PIC_BREAK)
  236.         if self.curline <> None and topline <= self.curline <= botline:
  237.             self.draw1icon(self.curline, self.piccurrent)
  238.             
  239.     def draw1icon(self, line, which):
  240.         offset = self.line_index[line]
  241.         botx, boty = self.ted.TEGetPoint(offset)
  242.         rect = self.rect[0]+2, boty-self.ted.lineHeight, \
  243.             self.rect[0]+ICONSIZE-2, boty
  244.         if not picture_cache.has_key(which):
  245.             picture_cache[which] = Qd.GetPicture(which)
  246.         self.drawicon(rect, picture_cache[which])
  247.         
  248.     def drawicon(self, rect, which):
  249.         Qd.DrawPicture(which, rect)
  250.  
  251. class MT_IndexList:
  252.     def __init__(self, wid, rect, width):
  253.         # wid is the window (dialog) where our list is going to be in
  254.         # rect is it's item rectangle (as in dialog item)
  255.         self.rect = rect
  256.         rect2 = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-1
  257.         self.list = List.LNew(rect2, (0, 0, width, 0), (0,0), 0, wid,
  258.                     0, 1, 0, 1)
  259.         self.wid = wid
  260.         self.width = width
  261.     
  262.     def setcontent(self, *content):
  263.         self.list.LDelRow(0, 1)
  264.         self.list.LSetDrawingMode(0)
  265.         self.list.LAddRow(len(content[0]), 0)
  266.         for x in range(len(content)):
  267.             column = content[x]
  268.             for y in range(len(column)):
  269.                 self.list.LSetCell(column[y], (x, y))
  270.         self.list.LSetDrawingMode(1)
  271.         Win.InvalRect(self.rect)
  272.  
  273.     def deselectall(self):
  274.         while 1:
  275.             ok, pt = self.list.LGetSelect(1, (0,0))
  276.             if not ok: return
  277.             self.list.LSetSelect(0, pt)
  278.             
  279.     def select(self, num):
  280.         self.deselectall()
  281.         if num < 0:
  282.             return
  283.         for i in range(self.width):
  284.             self.list.LSetSelect(1, (i, num))
  285.             
  286.     def click(self, where, modifiers):
  287.         is_double = self.list.LClick(where, modifiers)
  288.         ok, (x, y) = self.list.LGetSelect(1, (0, 0))
  289.         if ok:
  290.             return y, is_double
  291.         else:
  292.             return None, is_double
  293.             
  294.     # draw a frame around the list, List Manager doesn't do that
  295.     def drawframe(self):
  296.         Qd.SetPort(self.wid)
  297.         Qd.FrameRect(self.rect)
  298.         
  299.     def update(self, rgn):
  300.         self.drawframe()
  301.         self.list.LUpdate(rgn)
  302.         
  303.     def activate(self, onoff):
  304.         self.list.LActivate(onoff)
  305.         
  306. class MT_AnyList(MT_IndexList):
  307.  
  308.     def click(self, where, modifiers):
  309.         is_double = self.list.LClick(where, modifiers)
  310.         ok, (x, y) = self.list.LGetSelect(1, (0, 0))
  311.         if ok:
  312.             self.select(y)
  313.             field0 = self.list.LGetCell(1000,(0,y))
  314.         else:
  315.             field0 = None
  316.         return field0, is_double
  317.     
  318.